<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="真实姓名" prop="realName">
        <el-input
          v-model="queryParams.realName"
          placeholder="请输入真实姓名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入联系电话"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createdAt">
        <el-date-picker clearable
          v-model="queryParams.createdAt"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Check"
          :disabled="multiple"
          @click="handleBatchApprove"
          v-hasPermi="['application:application:review']"
        >一键通过审核</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Close"
          :disabled="multiple"
          @click="handleBatchReject"
          v-hasPermi="['application:application:review']"
        >一键拒绝审核</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['application:application:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="applicationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="申请ID" align="center" prop="applicationId" width="80" />
      <el-table-column label="真实姓名" align="center" prop="realName" width="100" />
      <el-table-column label="联系电话" align="center" prop="phone" width="120" />
      <el-table-column label="身份证正面照片" align="center" prop="idCardFront" width="120">
        <template #default="scope">
          <image-preview :src="scope.row.idCardFront" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="身份证背面照片" align="center" prop="idCardBack" width="120">
        <template #default="scope">
          <image-preview :src="scope.row.idCardBack" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="期望服务区域" align="center" prop="serviceArea" width="120" />
      <el-table-column label="个人介绍" align="center" prop="introduction" width="150" show-overflow-tooltip />
      <el-table-column label="相关经验" align="center" prop="experience" width="150" show-overflow-tooltip />
      <el-table-column label="申请状态" align="center" prop="applicationStatus" width="100">
        <template #default="scope">
          <el-tag v-if="scope.row.applicationStatus === 0" type="warning">待审核</el-tag>
          <el-tag v-else-if="scope.row.applicationStatus === 1" type="success">已通过</el-tag>
          <el-tag v-else-if="scope.row.applicationStatus === 2" type="danger">已拒绝</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="拒绝原因" align="center" prop="rejectReason" width="150" show-overflow-tooltip />
      <el-table-column label="审核人" align="center" prop="reviewerId" width="100" />
      <el-table-column label="创建时间" align="center" prop="createdAt" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createdAt, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核时间" align="center" prop="reviewTime" width="180">
        <template #default="scope">
          <span v-if="scope.row.reviewTime">{{ parseTime(scope.row.reviewTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          <span v-else style="color: #909399;">暂无</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updatedAt" width="180">
        <template #default="scope">
          <span v-if="scope.row.updatedAt">{{ parseTime(scope.row.updatedAt, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          <span v-else style="color: #909399;">暂无</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="280">
        <template #default="scope">
          <el-button
            v-if="scope.row.applicationStatus === 0"
            link
            type="success"
            icon="Check"
            @click="handleApprove(scope.row)"
            v-hasPermi="['application:application:review']">
            通过审核
          </el-button>
          <el-button
            v-if="scope.row.applicationStatus === 0"
            link
            type="danger"
            icon="Close"
            @click="handleReject(scope.row)"
            v-hasPermi="['application:application:review']">
            不通过审核
          </el-button>
          <el-button
            link
            type="danger"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['application:application:remove']">
            删除
          </el-button>
          <span v-if="scope.row.applicationStatus !== 0" class="text-gray-400" style="margin-left: 8px;">已审核</span>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 拒绝审核对话框 -->
    <el-dialog title="拒绝审核" v-model="reviewOpen" width="500px" append-to-body>
      <el-form ref="reviewRef" :model="reviewForm" :rules="reviewRules" label-width="80px">
        <el-form-item label="申请人">
          <el-input v-model="reviewForm.realName" readonly />
        </el-form-item>
        <el-form-item label="联系电话">
          <el-input v-model="reviewForm.phone" readonly />
        </el-form-item>
        <el-form-item label="拒绝原因" prop="rejectReason">
          <el-input v-model="reviewForm.rejectReason" type="textarea" placeholder="请输入拒绝原因" rows="4" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitReject">确 定</el-button>
          <el-button @click="reviewOpen = false">取 消</el-button>
        </div>
      </template>
    </el-dialog>


  </div>
</template>

<script setup name="Application">
import { listApplication, reviewApplication, batchApproveApplications, batchRejectApplications, delApplication } from "@/api/application/application"

const { proxy } = getCurrentInstance()

const applicationList = ref([])
const reviewOpen = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const reviewForm = ref({})



const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    realName: null,
    phone: null,
    createdAt: null
  },
  reviewRules: {
    rejectReason: [
      { required: true, message: "拒绝原因不能为空", trigger: "blur" }
    ]
  }
})

const { queryParams, reviewRules } = toRefs(data)

/** 查询红娘服务申请列表 */
function getList() {
  loading.value = true
  listApplication(queryParams.value).then(response => {
    applicationList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.applicationId)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 通过审核 */
function handleApprove(row) {
  proxy.$modal.confirm('是否确认通过"' + row.realName + '"的红娘服务申请？').then(function() {
    const reviewData = {
      applicationId: row.applicationId,
      applicationStatus: 1,
      rejectReason: ''
    }
    return reviewApplication(reviewData)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("审核通过成功")
  }).catch(() => {})
}

/** 拒绝审核 */
function handleReject(row) {
  reviewForm.value = {
    applicationId: row.applicationId,
    realName: row.realName,
    phone: row.phone,
    applicationStatus: 2,
    rejectReason: ''
  }
  reviewOpen.value = true
}

/** 提交拒绝审核 */
function submitReject() {
  proxy.$refs["reviewRef"].validate(valid => {
    if (valid) {
      reviewApplication(reviewForm.value).then(response => {
        proxy.$modal.msgSuccess("审核拒绝成功")
        reviewOpen.value = false
        getList()
      })
    }
  })
}

/** 批量通过审核 */
function handleBatchApprove() {
  const applicationIds = ids.value
  proxy.$modal.confirm('是否确认批量通过选中的红娘服务申请？').then(function() {
    return batchApproveApplications(applicationIds)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("批量审核通过成功")
  }).catch(() => {})
}

/** 批量拒绝审核 */
function handleBatchReject() {
  const applicationIds = ids.value
  proxy.$modal.confirm('是否确认批量拒绝选中的红娘服务申请？').then(function() {
    return batchRejectApplications(applicationIds)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("批量审核拒绝成功")
  }).catch(() => {})
}

/** 删除按钮操作 */
function handleDelete(row) {
  proxy.$modal.confirm('是否确认删除申请人"' + row.realName + '"的红娘服务申请？').then(function() {
    return delApplication(row.applicationId)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}



/** 导出按钮操作 */
function handleExport() {
  proxy.download('application/application/export', {
    ...queryParams.value
  }, `application_${new Date().getTime()}.xlsx`)
}

getList()
</script>

<style scoped>
.certification-images {
  display: flex;
  flex-direction: column;
}

.certification-images .el-image {
  border: 1px solid #dcdfe6;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.3s;
}

.certification-images .el-image:hover {
  border-color: #409eff;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.certification-labels {
  margin-top: 5px;
}
</style>
